package linkedlist.MyDLinkedList;

import javax.rmi.ssl.SslRMIClientSocketFactory;

class Node {
    //元素
    public int data;
    public Node prev = null;
    public Node next = null;

    public Node(int data) {
        this.data = data;
        next = null;
    }
}

public class MyDLinkedList {
    //头结点
    public Node head;
    //尾结点
    public Node tail;
    public MyDLinkedList() {
        head = null;
        tail = null;
    }
    //头插法
    public void addFirst(int data) {
        if (head == null&&tail == null) {
            Node newNode = new Node(data);
            head = newNode;
            tail = newNode;
            return;
        }
        Node newNode = new Node(data);
        newNode.next = head;
        head.prev = newNode;
        head = newNode;
    }
    //尾插法
    public void addLast(int data) {
        if (head == null&&tail == null) {
            Node newNode = new Node(data);
            head = newNode;
            tail = newNode;
            return;
        }
        Node newNode = new Node(data);
        tail.next = newNode;
        newNode.prev = tail;
        tail = newNode;
    }
    //任意位置插入,第一个数据节点为0号下标
    public boolean addIndex(int index,int data) {
        if (head == null&&tail == null) {
            Node newNode = new Node(data);
            head = newNode;
            tail = newNode;
            return true;
        }
        int size = size();
        if (index < 0||index > size) {
            return false;
        }
        if (index == 0) {
            addFirst(data);
            return true;
        }
        if (index == size) {
            addLast(data);
            return true;
        }
        Node newNode = new Node(data);
        Node cur = head;
        for (int i = 0;i < index;i++) {
            cur = cur.next;
        }
        cur.prev.next = newNode;
        newNode.prev = cur.prev;
        cur.prev = newNode;
        newNode.next = cur;
        return true;
    }
    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key) {
        Node cur = head;
        for (;cur != null;cur = cur.next) {
            if (cur.data == key) {
                return true;
            }
        }
        return false;
    }
    //删除第一次出现关键字为key的节点
    public void remove(int key) {
        Node cur = head;
        for (;cur != null;cur = cur.next) {
            if (cur.data == key) {
                if (cur.next == null) {
                    tail = tail.prev;
                    tail.next = null;
                    return;
                }
                if (cur.prev == null) {
                    head = head.next;
                    head.prev = null;
                    return;
                }
                cur.next.prev = cur.prev;
                cur.prev.next = cur.next;
                return;
            }
        }
    }
    //删除所有值为key的节点
    public void removeAllKey(int key) {
        if (head == null&&tail == null) {
            return;
        }
        Node cur = head;
        for (;cur != null;cur = cur.next) {
            if (cur.data == key) {
                if (cur.next == null) {
                    tail = tail.prev;
                    tail.next = null;
                    continue;
                }
                if (cur.prev == null) {
                    head = head.next;
                    head.prev = null;
                    continue;
                }
                cur.next.prev = cur.prev;
                cur.prev.next = cur.next;
                cur = cur.prev;
            }
        }
    }
    //得到单链表的长度
    public int size() {
        int size = 0;
        Node cur = head;
        for (;cur != null;cur = cur.next) {
            size++;
        }
        return size;
    }
    public void display() {
        Node cur = head;
        System.out.print("[");
        for (;cur != null;cur = cur.next) {
            System.out.print(cur.data);
            if (cur.next != null) {
                System.out.print(",");
            }
        }
        System.out.println("]");
    }
    public void clear() {
        head = null;
        tail = null;
    }

    public String toString() {
        if (head == null&&tail == null) {
            return "";
        }
        StringBuilder stringBuilder = new StringBuilder();
        Node cur = head;
        stringBuilder.append("[");
        for (;cur != null;cur = cur.next) {
            stringBuilder.append(cur.data);
            if (cur.next != null) {
                stringBuilder.append(",");
            }
        }
        stringBuilder.append("]");
        return stringBuilder.toString();
    }
}
